package com.zjcloud.academic.controller;

import com.zjcloud.academic.domain.AcCourseSchedule;
import com.zjcloud.academic.mapper.AcCourseScheduleMapper;
import com.zjcloud.academic.service.IntelligentScheduleService;
import com.zjcloud.common.core.domain.AjaxResult;
import com.zjcloud.common.core.domain.entity.SysUser;
import com.zjcloud.common.utils.SecurityUtils;

import java.util.List;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 智能排课控制器
 * 
 * @author zjcloud
 * @date 2024-01-01
 */
@Api("智能排课管理")
@RestController
@RequestMapping("/academic/intelligentSchedule")
public class IntelligentScheduleController {

    @Autowired
    private IntelligentScheduleService intelligentScheduleService;
    
    @Autowired
    private AcCourseScheduleMapper courseScheduleMapper;

    /**
     * 智能排课
     */
    @ApiOperation("智能排课")
    @PreAuthorize("@ss.hasPermi('academic:scheduleTask:intelligent')")
    @PostMapping("/{semesterId}")
    public AjaxResult intelligentSchedule(
            @ApiParam("学期ID") @PathVariable Long semesterId) {
        try {
            List<AcCourseSchedule> result = intelligentScheduleService.intelligentSchedule(semesterId);
            return AjaxResult.success(result);
        } catch (Exception e) {
            return AjaxResult.error("智能排课失败：" + e.getMessage());
        }
    }

    /**
     * 检查排课冲突
     */
    @ApiOperation("检查排课冲突")
    @PreAuthorize("@ss.hasPermi('academic:scheduleTask:intelligent')")
    @GetMapping("/checkConflict/{semesterId}")
    public AjaxResult checkConflict(@ApiParam("学期ID") @PathVariable Long semesterId) {
        try {
            // 获取指定学期的所有排课
            AcCourseSchedule query = new AcCourseSchedule();
            query.setSemesterId(semesterId);
            List<AcCourseSchedule> schedules = courseScheduleMapper.selectAcCourseScheduleList(query);
            
            // 检查每个排课的冲突
            int totalConflicts = 0;
            for (AcCourseSchedule schedule : schedules) {
                List<String> conflicts = intelligentScheduleService.checkScheduleConflict(schedule);
                totalConflicts += conflicts.size();
            }
            
            return AjaxResult.success(totalConflicts);
        } catch (Exception e) {
            return AjaxResult.error("检查冲突失败：" + e.getMessage());
        }
    }

    /**
     * 获取排课统计信息
     */
    @ApiOperation("获取排课统计信息")
    @PreAuthorize("@ss.hasPermi('academic:scheduleTask:intelligent')")
    @GetMapping("/statistics/{semesterId}")
    public AjaxResult getStatistics(@ApiParam("学期ID") @PathVariable Long semesterId) {
        try {
            IntelligentScheduleService.ScheduleStatistics statistics = 
                intelligentScheduleService.getScheduleStatistics(semesterId);
            return AjaxResult.success(statistics);
        } catch (Exception e) {
            return AjaxResult.error("获取统计信息失败：" + e.getMessage());
        }
    }
}